package xyz.yuanjilu.plug.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.riversoft.weixin.common.event.EventRequest;
import com.riversoft.weixin.common.event.EventType;
import com.riversoft.weixin.common.message.XmlMessageHeader;
import com.riversoft.weixin.common.message.xml.TextXmlMessage;

import xyz.yuanjilu.entity.WeixinConfigEntity;
import xyz.yuanjilu.entity.WeixinUserEntity;
import xyz.yuanjilu.plug.MPPlug;
import xyz.yuanjilu.service.WeixinConfigService;
import xyz.yuanjilu.service.WeixinUserService;

/**
 * 
 * @author chenjunfei
 *
 */
@Component
public class WelComeMPPlugImpl implements MPPlug{
	
	@Autowired
    private WeixinConfigService weixinConfigService;
	@Autowired
	private WeixinUserService weixinUserService; 
	
	@Override
	public boolean superWeixinCode(WeixinConfigEntity weixinConfig) {
		return true;//支持全部微信
	}

	@Override
	public boolean superXmlMessageHeader(XmlMessageHeader xmlRequest, WeixinConfigEntity weixinConfig) {
		if (xmlRequest instanceof EventRequest) {
            EventRequest eventRequest = (EventRequest) xmlRequest;
            EventType eventType = eventRequest.getEventType();
            if (EventType.subscribe.equals(eventType)||EventType.unsubscribe.equals(eventType)) {//只支持关注事件
            	
            	return true;
            	
            	//toMessage.setContent(weixinConfigService.getWelCome(weixinConfig));
			}
        }
		return false;
	}

	@Override
	public String getTextMenu(WeixinConfigEntity weixinConfig) {
		return "微信欢迎文字菜单";
	}

	@Override
	public String getDescribe(WeixinConfigEntity weixinConfig) {
		//事件一般不给提示信息
		return "";
	}

	@Override
	public XmlMessageHeader handle(String weixinUserId, XmlMessageHeader xmlRequest, XmlMessageHeader xmlResponse,
			WeixinConfigEntity weixinConfig) {
		
		if (xmlRequest instanceof EventRequest) {
            EventRequest eventRequest = (EventRequest) xmlRequest;
            EventType eventType = eventRequest.getEventType();
            WeixinUserEntity weixinUser =new WeixinUserEntity();
            weixinUser.setId(weixinUserId);
            weixinUser.setWeixinId(weixinConfig.getId());
            weixinUser.setOpenid(weixinUserId);
            if (EventType.subscribe.equals(eventType)) {//只支持关注事件
            	weixinUser.setStatusType(EventType.subscribe.name());
            	weixinUserService.saveOrUpdate(weixinUser);
            	if (xmlResponse instanceof TextXmlMessage) {
        			TextXmlMessage response = (TextXmlMessage) xmlResponse;
        			String text="\n"+response.getContent();
        			text+=weixinConfigService.getWelCome(weixinConfig);
        			response.setContent(text);
        			return response;
        		}else{
        			return xmlResponse;
        		}
            
            }else if (EventType.unsubscribe.equals(eventType)) {
            	weixinUser.setStatusType(EventType.unsubscribe.name());
            	weixinUserService.saveOrUpdate(weixinUser);
			}
        }
		return xmlResponse;
		
	}

	@Override
	public String getName(WeixinConfigEntity weixinConfig) {
		return "关注自动回复插件";
	}
	
}
